#!/usr/bin/env python
# coding: utf-8

# In[1]:


#导入必要条件
import requests
import pandas as pd
ldq_key = '37d910e782c83fc25f060611af2cab2a'


# ## 地理编码

# In[2]:


#定义函数geocode
#输入所有参数
def geocode(address, city=None, batch=None, sig=None, output=None, callback=None):
    #地理编码 API 服务地址
    url = 'https://restapi.amap.com/v3/geocode/geo?parameters' 
    params = {
        'key': ldq_key,
        'address': address,
        'city': city,
        'batch': batch,
        'sig': sig,
        'output': output, """响应结果的格式可以通过请求参数 output 指定，默认为 JSON 形式。"""
        'callback': callback
    }
#    url的请求方式是get
    response = requests.get(url, params= params) 
    data = response.json()
    return data


# ## 逆地理编码

# In[3]:


#定义函数regeocode
def regeocode(location, poitype=None, radius=None, extensions=None, batch=None, roadlevel=None, sig=None, output=None, callback=None, homeorcorp=None):
    #逆地理编码 API 服务地址
    url = 'https://restapi.amap.com/v3/geocode/regeo?parameters'
    params = {
        'key': ldq_key,
        'location': location,
        'poitype': poitype,
        'radius': radius,
        'extensions': extensions,
        '''extensions 参数默认取值是 base，也就是返回基本地址信息；
        extensions 参数取值为 all 时会返回基本地址信息、附近 POI 内容、道路信息以及道路交叉口信息。'''
        'batch': batch,
        'roadlevel': roadlevel,
        'homeorcorp': homeorcorp,
        'sig': sig,
        'output': output,
        'callback': callback
    }
    #    url的请求方式是get
    response = requests.get(url, params=params)
    data = response.json()
    return data


# ## 路径规划

# In[4]:


#3.1步行路径规划--步行路径规划 API 可以规划100KM以内的步行通勤方案，并且返回通勤方案的数据。
#定义函数walking
def walking(origin, destination, sig=None, output=None, callback=None):
    #步行路径规划API URL
    url = 'https://restapi.amap.com/v3/direction/walking?parameters'
    params = {
        'key': ldq_key,
        'origin': origin,
        'destination': destination,
        'sig': sig,
        'output': output,
        'callback': callback
    }
    response = requests.get(url, params=params)
    data = response.json()
    return data


# In[5]:


#3.2公交路径规划--公交路径规划 API 可以规划综合各类公共（火车、公交、地铁）交通方式的通勤方案，并且返回通勤方案的数据。
#定义函数transit
def transit(origin, destination, city, cityd=None, extensions=None, strategy=None, nightflag=None, date=None, time=None, sig=None, output=None, callback=None):
    #公交路径规划API URL：
    url = 'https://restapi.amap.com/v3/direction/transit/integrated?parameters'
    params = {
        'key': ldq_key,
        'origin': origin,
        'destination': destination,
        'city': city,
        'cityd': cityd,
        'extensions': extensions,
        'strategy': strategy,
        'nightflag':nightflag,
        'date': date,
        'time': time,
        'sig': sig,
        'output': output,
        'callback': callback
    }
    response = requests.get(url, params=params)
    data = response.json()
    return data


# In[6]:


#3.3驾车路径规划--驾车路径规划 API 可以规划以小客车、轿车通勤出行的方案，并且返回通勤方案的数据。
#定义函数driving
def driving(origin, destination, extensions, originid=None, destinationid=None, origintype=None, destinationtype=None, strategy=None, waypoints=None, avoidpolygons=None, province=None, number=None, cartype=None, ferry=None, roadaggregation=None, nosteps=None, sig=None, output=None, callback=None):
    #驾车路径规划API URL：
    url = 'https://restapi.amap.com/v3/direction/driving?parameters'
    params = {
        'key':ldq_key,
        'origin':origin,
        'destination':destination,
        'originid':originid,
        'destinationid':destinationid,
        'origintype':origintype,
        'destinationtype':destinationtype,
        'strategy':strategy,
        'waypoints': waypoints,
        'avoidpolygons':avoidpolygons,
        'province':province,
        'number':number,
        'cartype':cartype,
        'ferry':ferry,
        'roadaggregation':roadaggregation,
        'nosteps': nosteps,
        'sig':sig,
        'output':output,
        'callback':callback,
        'extensions':extensions
    }
    response = requests.get(url, params=params)
    data = response.json()
    return data


# In[7]:


#3.4骑行路径规划--骑行路径规划用于规划骑行通勤方案，规划时不会考虑路况；考虑天桥、单行线、封路等情况。
#定义函数bicycling
def bicycling(origin, destination):
    #骑行路径规划API URL:
    url = 'https://restapi.amap.com/v4/direction/bicycling?parameters'
    params = {
        'key': ldq_key,
        'origin': origin,
        'destination': destination
    }
    response = requests.get(url, params=params)
    data = response.json()
    return data


# In[8]:


#3.5货车路径规划
#定义函数truck
def truck(origin, destination, size, originid=None, destinationid=None, origintype=None, destinationtype=None, diu=None, strategy=None, waypoints=None, height=None, width=None, load=None, weight=None, axis=None, province=None, number=None, cartype=None, avoidpolygons=None, showpolyline=None, nosteps=None):
    #货车路径规划API URL：
    url = 'https://restapi.amap.com/v4/direction/truck?parameters'
    params = {
        'key': ldq_key,
        'origin': origin,
        'destination':destination,
        'size': size,
        'originid':originid,
        'destinationid':destinationid,
        'origintype':origintype,
        'destinationtype':destinationtype,
        'diu': diu,
        'strategy':strategy,
        'waypoints': waypoints,
        'height': height,
        "weight": weight,
        'width': width,
        'load': load,
        'axis': axis, 
        'province':province,
        'number':number,
        'cartype':cartype,
        'avoidpolygons':avoidpolygons,
        'showpolyline': showpolyline,
        'nosteps': nosteps       
    }
    response = requests.get(url, params=params)
    data = response.json()
    return data


# In[9]:


#3.6距离测量
#定义函数distance
def distance(origins, destination, type=None, sig=None, output=None, callback=None):
    #距离测量API URL：
    url = 'https://restapi.amap.com/v3/distance?parameters'
    params = {
        'key': ldq_key,
        'origins': origins,
        'destination': destination,
        'type': type,
        'sig': sig,
        'output': output,
        'callback': callback
    }
    response = requests.get(url, params=params)
    data = response.json()
    return data


# In[10]:


#物流距离测量矩阵。物流长途距离计算矩阵是物流API服务收费接口


# ## 行政区域查询

# In[11]:


#适用场景：用户希望通过得到行政区域信息，进行开发工作。
#定义函数administration
def administration(keywords=None, subdistrict=None, page=None, offset=None, extensions=None, filter=None, callback=None, output=None):
    #行政区域查询API服务地址：
    url = 'https://restapi.amap.com/v3/config/district?parameters'
    params ={
        'key': ldq_key,
        'keywords': keywords,
        'subdistrict': subdistrict,
        'page': page,
        'offset': offset,
        'extensions': extensions,
        'filter': filter,
        'output': output,
        'callback': callback
    }
    response = requests.get(url, params=params)
    data = response.json()
    return data


# ## 搜索POI

# In[12]:


#关键字搜索--通过用POI的关键字进行条件搜索，例如：肯德基、朝阳公园等；同时支持设置POI类型搜索，例如：银行
#定义函数text
def text(keywords, types, city=None, citylimit=None, children=None, page=None, extensions=None, sig=None, output=None, callback=None):
    #关键字搜索API服务地址：
    url = 'https://restapi.amap.com/v3/place/text?parameters'
    params = {
        'key': ldq_key,
        'keywords': keywords,
        'types': types,
        'city': city,
        'citylimit': citylimit,
        'children': children,
        'page': page,
        'extensions': extensions,
        'output': output,
        'callback': callback
    }
    response = requests.get(url, params=params)
    data = response.json()
    return data


# In[13]:


#周边搜索--在用户传入经纬度坐标点附近，在设定的范围内，按照关键字或POI类型搜索
#定义函数around
def around(location, keywords=None, types=None, city=None, redius=None, sortrule=None, offset=None, page=None, extensions=None, sig=None, output=None, callback=None):
    #周边搜索API服务地址：
    url = 'https://restapi.amap.com/v3/place/around?parameters '
    params = {
        'key': ldq_key,
        'location': location,
        'keywords': keywords,
        'types': types,
        'city': city,
        'redius': redius,
        'sortrule': sortrule,
        'offset': offset,
        'page': page,
        'extensions': extensions,
        'sig': sig,
        'output': output,
        'callback': callback
    }
    response = requests.get(url, params=params)
    data = response.json()
    return data


# In[14]:


#多边形搜索--在多边形区域内进行搜索
#定义函数polygon
def polygon(polygon, keywords= None, types=None, offset=None, page=None, extensions=None, sig=None, output=None, callback=None):
    #多边形搜索API服务地址：
    url = 'https://restapi.amap.com/v3/place/polygon?parameters '
    params = {
        'key': ldq_key,
        'polygon': polygon,
        'keywords': keywords,
        'types': types,
        'offset': offset,
        'page': page,
        'extensions': extensions,
        'sig': sig,
        'output': output,
        'callback': callback
    }
    response = requests.get(url, params=params)
    data = response.json()
    return data


# In[15]:


#ID查询--通过POI ID，查询某个POI详情，建议可同输入提示API配合使用
#定义函数id
def detail(id, sig=None, output=None, callback=None):
    #ID查询搜索API服务地址：
    url = 'https://restapi.amap.com/v3/place/detail?parameters'
    params = {
        'key': ldq_key,
        'id': id,
        'output': output,
        'sig': sig,
        'callback': callback
    }
    response = requests.get(url, params=params)
    data = response.json()
    return data


# ## IP定位

# In[16]:


#希望能够将IP信息转换为地理位置信息。
#定义函数iipp
def iipp(ip=None, sig=None, output='JSON'):
    url = 'https://restapi.amap.com/v3/ip?parameters'
    params = {
        'key': ldq_key,
        'ip': ip,
        'sig': sig,
        'output': output
    }
    response = requests.get(url, params=params)
    data = response.json()
    return data


# ## 静态地图

# In[17]:


#定义函数staticmap
from PIL import Image
from io import BytesIO
def staticmap(location, zoom, size=None, scale=1, markers=None, labels=None, paths=None, traffic=None, sig=None):
    url = 'https://restapi.amap.com/v3/staticmap?parameters'
    params = {
        'key': ldq_key,
        'location': location,
        'zoom': zoom,
        'size': size,
        'scale': scale,
        'markers': markers,
        'labels': labels,
        'paths': paths,
        'traffic': traffic,
        'sig': sig
        
    }
    response = requests.get(url, params=params)
    data = Image.open(BytesIO(response.content))
    return data


# ## 坐标转换

# In[18]:


#为了使用高德服务，用户需要将非高德坐标转换为高德坐标。
#定义函数coordinate
def coordinate(locations, coordsys=None, sig=None, output=None):
    #坐标转换API服务地址：
    url = 'https://restapi.amap.com/v3/assistant/coordinate/convert?parameters'
    params = {
        'key': ldq_key,
        'locations': locations,
        'coordsys': coordsys,
        'sig': sig,
        'output': output
    }
    response = requests.get(url, params=params)
    data = response.json()
    return data


# ## 天气查询

# In[19]:


#需要使用相关天气查询的时候。
#定义函数weather
def weather(city, extensions=None, output=None):
    #天气查询API服务地址：
    url = "https://restapi.amap.com/v3/weather/weatherInfo?parameters"
    params = {
        'key': ldq_key,
        'city': city,
        'extensions': extensions,
        'output': output
    }
    response = requests.get(url, params=params)
    data = response.json()
    return data


# ## 输入提示

# In[20]:


#在高德客户端的使用场景，输入“关键词”之后出现提示相关。
#定义函数inputtips
def inputtips(keywords, type=None, location=None, city=None, citylimit=None, datatype=None, sig=None, output=None, callback=None):
    #输入提示API服务地址：
    url = 'https://restapi.amap.com/v3/assistant/inputtips?parameters'
    params = {
        'key': ldq_key,
        'keywords': keywords,
        'type': type,
        'location': location,
        'city': city,
        'citylimit': citylimit,
        'datatype': datatype, 
        'sig': sig,
        'output': output,
        'callback': callback
    }
    response = requests.get(url, params)
    data = response.json()
    return data
